<?php
class M_qin_events extends Cemvc_Db_MysqlDb{
  public $PrimaryKey='id';
  /*CEPHP会在要需要时查询主键，但主动设置主键可以使程序执行更快*/

  public function __construct()
  {
    parent::__construct();
  }
  public function get($id)
  {
    return $this->where(array("id = $id"))->fetchOne();
  }

  public function get_all()
  {
    return $this->fetchAll();  
  }
  /* get all the events with the specified user id */
  public function get_events($user_id, $state=RESOURCE_STATE_INIT)
  {
    $where = array();
    $where[] = "user_id = $user_id";
    if ($state)
      $where[] = "state = '$state'";
    return $this->where($where)->orderBy("date")->fetchAll();
  }

  public function clear_cache($event_id)
  {
    $event = $this->get($event_id);
    M_Tools::del_cache($this->LastQueryString);	

    /* get init state events */
    $this->get_events($event["user_id"]);
    M_Tools::del_cache($this->LastQueryString);	

    /* get all state events */
    $this->get_events($event["user_id"], null);
    M_Tools::del_cache($this->LastQueryString);	

    $history = new M_qin_history();
    $history->get_history_with_content(CONTENT_TYPE_EVENT, $event_id);
    M_Tools::del_cache($history->LastQueryString);
  }
  /* insert an event to events table */
  public function add(array $array)
  {
    $res = $this->insert(array("user_id" => $array["user_id"],
			       "date"    => $array["date"],
			       "event"   => addslashes($array["event"])));

    $event = $this->get($res["last_insert_id()"]);

    /* $this->get_events($array["user_id"]); */
    /* M_Tools::del_cache($this->LastQueryString);	 */
    $this->clear_cache($event["id"]);

    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_EVENT,
    			"content_id"   => $event["id"],
    			"content"      => base64_encode(serialize($event)),
    			"operate_type" => OPERATE_TYPE_ADD,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $event;
  }

  public function edit($id, $array)
  {
    $this->where(array("id = $id"))->update(array("event"=>addslashes($array["event"])));

    /* $event = $this->get($id); */
    /* M_Tools::del_cache($this->LastQueryString);	 */
    $this->clear_cache($id);

    $event = $this->get($id);

    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_EVENT,
    			"content_id"   => $event["id"],
    			"content"      => base64_encode(serialize($event)),
    			"operate_type" => OPERATE_TYPE_EDIT,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $event;
  }

  public function delete($id)
  {
    $this->where(array("id = $id"))->update(array("state"=>RESOURCE_STATE_DELETED));

    /* $event = $this->get($id); */
    /* M_Tools::del_cache($this->LastQueryString);	 */
    $this->clear_cache($id);

    $event = $this->get($id);

    /* add to history */
    $history = new M_qin_history();
    $history->add(array("content_type" => CONTENT_TYPE_EVENT,
    			"content_id"   => $event["id"],
    			"content"      => base64_encode(serialize($event)),
    			"operate_type" => OPERATE_TYPE_DELETE,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $event;
  }

  public function restore($event_id, $history_id)
  {
    /* get history data and set state to init */
    $history = new M_qin_history();
    $res = $history->get($history_id);
    $array = unserialize(base64_decode($res["content"]));
    $array["state"] = RESOURCE_STATE_INIT;

    /* update data */
    /* $e = $this->edit($event_id, $array); */
    $this->where(array("id = $event_id"))->update($array);

    /* clear cache */
    /* $event = $this->get($event_id); */
    /* M_Tools::del_cache($this->LastQueryString);	 */
    $this->clear_cache($event_id);

    /* add to history */
    $event = $this->get($event_id);
    $history->add(array("content_type" => CONTENT_TYPE_EVENT,
    			"content_id"   => $event["id"],
    			"content"      => base64_encode(serialize($event)),
    			"operate_type" => OPERATE_TYPE_RESTORE,
    			"author_id"    => $_SESSION["id"],
    			"author_name"  => $_SESSION["realname"]));

    return $event;
  }
}